<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();

$url = MiscUtils::getParam('url', NULL);
$page_id = MiscUtils::getParam('i', 0);
$inst_id = MiscUtils::getParam('inid', 0);
$folder = MiscUtils::getParam('infold', 0);

if (isset($_SESSION['admin' . USER])) {
  echo "
  <script>
  parent.parent.location.href = 'http://" . URL . "admin.php';
  </script>
  ";
  die('');
}

$myPdo = DbUtils::createPdoInst();

$system_id = 0;
$cond_vals = new stdClass();
$cond_vals->c = 't.id = :v1';
$cond_vals->v = array(':v1' => $page_id);
$system_id = DbUtils::get($myPdo, 'pz2_page', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d[0]->sys_id;

$cond_vals = new stdClass();
$cond_vals->c = 't.sys_id = :v1 AND t.parent_id = 0 AND t.is_visible = 1';
$cond_vals->v = array(':v1' => $system_id);
$pages = DbUtils::get($myPdo, 'pz2_page', $cond_vals, 't.order', 'ASC', NULL, NULL, NULL)->d;

?>
<link rel="stylesheet" type="text/css" href="css/demo.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<link rel="stylesheet" type="text/css" href="css/animate-custom.css" />
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  outline: 0;
  margin: 0;
}

body {
  background-image: url(assets/imgs/bg.png);
  background-repeat: repeat;
}
</style>
<section class="main">
  <form class="form-2">
    <h1><span class="log-in">Log in</span></h1>
    <p class="float">
      <label for="login"><i class="icon-user"></i>Username</label>
      <input id="account" type="text" name="login" placeholder="Username or email">
    </p>
    <p class="float">
      <label for="password"><i class="icon-lock"></i>Password</label>
      <input id="password" type="password" name="password" placeholder="Password" class="showpassword">
    </p>
    <p class="clearfix"> 
      <a style="visibility: collapse;" href="#" class="log-twitter">Log in with Twitter</a>    
      <input id="loginbtn" type="submit" name="submit" value="Log in">
    </p>
  </form>
</section>
<div>&nbsp;</div>
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
  $(function() {
    $('#loginbtn')[0].onclick = function(e) {
      login();
      e.preventDefault();
      return false;
    };
  });
  
  function login() {
    var obj = {
      account: $('#account')[0].value,
      password: $('#password')[0].value
    };
    $.ajax({
      type: 'POST',
      url: 'server.php',
      data: 'action=login&q=' + encodeURIComponent(JSON.stringify(obj)),
      success: function(msg){
        try {
          if (JSON.parse(msg).length > 0) {
            parent.location.href = '<?php echo ($url) ? $url : 'http://' . URL . 'admin.php?i=' . $pages[0]->id ; ?>';
          }
        } catch (ex) {
        }
      }
    });
  }
</script>
